package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.PrepareDetailResult;
import com.douwong.student.model.FeaturesBean;
import com.douwong.student.model.Prepare;
import com.douwong.student.model.PrepareBean;
import com.douwong.student.model.PrepareResource;
import com.douwong.student.model.PrepareTask;
import com.douwong.student.model.PrepareTest;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.NetUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class PrepareDetailTask extends AsyncTask<String, Integer, Integer> {

	private Prepare prepare;
	private ArrayList<FeaturesBean>features=new  ArrayList<FeaturesBean>();
	private ArrayList<PrepareTest>tests=new ArrayList<PrepareTest>();
	private ArrayList<PrepareTask>tasks=new ArrayList<PrepareTask>();
	private ArrayList<PrepareResource>resources =new ArrayList<PrepareResource>();
	private PrepareDetailResult detailResult;
	public static int REQUEST_OK=1;
	public static int REQUEST_ERROR=-1;
	private String TAG="PrepareDetailTask";
	
	@Override
	protected Integer doInBackground(String... params) {
		String response=null;
		int stateCode=REQUEST_OK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		try {
			Log.e("prepaareId",params[0]);
			arrayParams.add(new BasicNameValuePair("prepareId", params[0]));
			response= NetUtils.httpGet(NetConfig.PREPARE_INDRO_LIST, arrayParams);//导读
			Log.e("response",response);
			StatusCode<PrepareBean> sc = (StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			if(sc!=null&&sc.isFlag()){
//				prepare=sc.getData();
//				Log.e("name",prepare.getLessonName()+"");
//				features=(ArrayList<FeaturesBean>) JSON.parseArray(prepare.getListPlIntroductionBean(), FeaturesBean.class);
//				tests=(ArrayList<PrepareTest>) JSON.parseArray(prepare.getListPlExamineBean(),PrepareTest.class);
//				tasks=(ArrayList<PrepareTask>) JSON.parseArray(prepare.getListPlTaskBean(),PrepareTask.class);
//				resources=(ArrayList<PrepareResource>) JSON.parseArray(prepare.getListPlAttachmentBean(),PrepareResource.class);
//				Log.e("features",features.size()+"");
//				Log.e("tests",tests.size()+"");
//				Log.e("tasks",tasks.size()+"");
//				Log.e("resources",resources.size()+"");
//				if ((sc.getData().getRows() == null || sc.getData().getRows()
//						.equals(""))) {
//				}
				
				features = (ArrayList<FeaturesBean>) JSON.parseArray(sc
						.getData().getRows(), FeaturesBean.class);
			}
			response=NetUtils.httpGet(NetConfig.PREPARE_TEST_LIST, arrayParams);//测验
			sc=(StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			if(sc!=null&&sc.isFlag()){
				tests=(ArrayList<PrepareTest>) JSON.parseArray(sc
						.getData().getRows(), PrepareTest.class);
			}
			Log.e("response",response);
			
			response=NetUtils.httpGet(NetConfig.PREPARE_TASK_LIST, arrayParams);
			sc=(StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			if(sc!=null&&sc.isFlag()){
				tasks=(ArrayList<PrepareTask>) JSON.parseArray(sc
						.getData().getRows(), PrepareTask.class);
			}
//			Log.e("response",response);
			response=NetUtils.httpGet(NetConfig.PREPARE_RESOURCE_LIST, arrayParams);
			sc=(StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			if(sc!=null&&sc.isFlag()){
				resources=(ArrayList<PrepareResource>) JSON.parseArray(sc
						.getData().getRows(), PrepareResource.class);
			}
//			Log.e("response",response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return stateCode;
	}
	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		detailResult.detailNotify(result, features, resources, tests, tasks);
	}
	public PrepareDetailResult getDetailResult() {
		return detailResult;
	}
	public void setDetailResult(PrepareDetailResult detailResult) {
		this.detailResult = detailResult;
	}

}
